﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Common
{
    public class LogHelper
    {
        private static Boolean _isInit = false;
        private static AutoUpdateConfigModel serverModel;
        private static MongoDBHelper dbHelper;
        private static LocalXMLModel locaModel;
        private static void Init()
        {
            if (!_isInit)
            {
                serverModel = ClientAndServerHelper.Instance.GetUpdateContent();
                locaModel = ClientAndServerHelper.Instance.GetLocalXmlContent();
                dbHelper = new MongoDBHelper(HospitalConfig.Instance.CurrentHospital.Ip, HospitalConfig.Instance.CurrentHospital.Port, HospitalConfig.Instance.CurrentHospital.DbName, HospitalConfig.Instance.CurrentHospital.DbUser, HospitalConfig.Instance.CurrentHospital.DbPwd);
                string path = "";
                if (serverModel != null)
                {
                    path = serverModel.XuNiPath;//Environment.CurrentDirectory;//存放Log.config的路径 
                }

                FileInfo file = new FileInfo(path + @"Log.config");

                if (File.Exists(file.ToString()))
                {
                    log4net.Config.XmlConfigurator.Configure(file);
                }
                else
                {
                    SaveToDB("LogHelper", "日志配置文件没有找到");
                }
                _isInit = true;
            }
        }
        /// <summary>
        /// 输出日志到Log4Net
        /// </summary>
        /// <param name="t"></param>
        /// <param name="ex"></param>
            #region static void Error(Type t, Exception ex)

        public static void Error(Type t, Exception ex)
        {
            Init();
            log4net.ILog log = log4net.LogManager.GetLogger(t);
            log.Error("Error", ex);
            SaveToDB(ex);
        }

        public static void Error(string errorType,Type t, Exception ex)
        {
            Init();
            log4net.ILog log = log4net.LogManager.GetLogger(t);
            log.Error("Error", ex);
            SaveToDB(ex,errorType);
        }


        #endregion

        /// <summary>
        /// 输出日志到Log4Net
        /// </summary>
        /// <param name="t"></param>
        /// <param name="ex"></param>
        #region static void Error(string name, Exception ex)

        public static void Error(string name, Exception ex)
        {
            Init();
            log4net.ILog log = log4net.LogManager.GetLogger(name);
            log.Error("Error", ex);
            SaveToDB(ex);
        }

        #endregion

        /// <summary>
        /// 输出日志到Log4Net
        /// </summary>
        /// <param name="t"></param>
        /// <param name="msg"></param>
        #region static void Error(Type t, string msg)

        public static void Error(Type t, string msg)
        {
            Init();
            log4net.ILog log = log4net.LogManager.GetLogger(t);
            log.Error(msg);
            SaveToDB(t.FullName, msg);
        }
        #endregion
        /// <summary>
        /// 输出日志到Log4Net
        /// </summary>
        /// <param name="t"></param>
        /// <param name="ex"></param>
        #region static void Debug(Type t, Exception ex)

        public static void Debug(Type t, Exception ex)
        {
            Init();
            log4net.ILog log = log4net.LogManager.GetLogger(t);
            log.Debug("Debug", ex);
            SaveToDB(ex);
        }

        #endregion

        /// <summary>
        /// 输出日志到Log4Net
        /// </summary>
        /// <param name="t"></param>
        /// <param name="msg"></param>
        #region static void Debug(Type t, string msg)

        public static void Debug(Type t, string msg)
        {
            Init();
            log4net.ILog log = log4net.LogManager.GetLogger(t);
            log.Debug(msg);
            SaveToDB(t.FullName, msg);
        }
        #endregion
        /// <summary>
        /// 输出日志到Log4Net
        /// </summary>
        /// <param name="t"></param>
        /// <param name="ex"></param>
        #region static void Info(Type t, Exception ex)

        public static void Info(Type t, Exception ex)
        {
            Init();
            log4net.ILog log = log4net.LogManager.GetLogger(t);
            log.Info("Info", ex);
            SaveToDB(ex);
        }

        #endregion

        /// <summary>
        /// 输出日志到Log4Net
        /// </summary>
        /// <param name="t"></param>
        /// <param name="msg"></param>
        #region static void Info(Type t, string msg)

        public static void Info(Type t, string msg)
        {
            Init();
            log4net.ILog log = log4net.LogManager.GetLogger(t);
            log.Info(msg);
            SaveToDB(t.FullName, msg);
        }

        #endregion
        /// <summary>
        /// 输出日志到Log4Net
        /// </summary>
        /// <param name="t"></param>
        /// <param name="ex"></param>
        #region static void Warn(Type t, Exception ex)

        public static void Warn(Type t, Exception ex)
        {
            Init();
            log4net.ILog log = log4net.LogManager.GetLogger(t);
            log.Warn("Warn", ex);
            SaveToDB(ex);
        }

        #endregion

        /// <summary>
        /// 输出日志到Log4Net
        /// </summary>
        /// <param name="t"></param>
        /// <param name="msg"></param>
        #region static void Warn(Type t, string msg)

        public static void Warn(Type t, string msg)
        {
            Init();
            log4net.ILog log = log4net.LogManager.GetLogger(t);
            log.Warn(msg);
            SaveToDB(t.FullName, msg);
        }
        #endregion
        /// <summary>
        /// 输出日志到Log4Net
        /// </summary>
        /// <param name="t"></param>
        /// <param name="ex"></param>
        #region static void Fatal(Type t, Exception ex)

        public static void Fatal(Type t, Exception ex)
        {
            Init();
            log4net.ILog log = log4net.LogManager.GetLogger(t);
            log.Fatal("Fatal", ex);
            SaveToDB(ex);
        }

        #endregion

        /// <summary>
        /// 输出日志到Log4Net
        /// </summary>
        /// <param name="t"></param>
        /// <param name="msg"></param>
        #region static void Fatal(Type t, string msg)

        public static void Fatal(Type t, string msg)
        {
            Init();
            log4net.ILog log = log4net.LogManager.GetLogger(t);
            log.Fatal(msg);
            SaveToDB(t.FullName, msg);
        }

        #endregion

        private static void SaveToDB(string className, string message)
        {
            try
            {
                LogDBModel model = new LogDBModel();
                model.LocalModel = locaModel;
                model.ClassName = className;
                model.CreateTime = DateTime.Now;
                model.Message = message;
                model.LogType = "info";
                dbHelper.Insert(model, "Logs");
            }
            catch (Exception)
            {
                
            }
        }
        private static void SaveToDB(Exception ex,string errorType="error")
        {
            try
            {
                LogDBModel model = new LogDBModel();
                model.LocalModel = locaModel;
                model.ClassName = ex.Source;
                model.CreateTime = DateTime.Now;
                model.Message = ex.Message;
                model.LogType = errorType;
                model.StackTrace = ex.StackTrace;

                dbHelper.Insert(model, "Logs");
            }
            catch (Exception)
            {
                
            }
        }
    }
}
